package com.note.servlet.view;

import com.alibaba.fastjson.JSON;
import com.note.pojo.Notetype;
import com.note.pojo.page;
import com.note.service.note.noteService;
import com.note.service.note.noteServiceImpl;
import lombok.SneakyThrows;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class allNote extends HttpServlet {
    @SneakyThrows
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html");
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        String currentPage = req.getParameter("currentPage");
        int pageSize = 5;
        noteService noteService = new noteServiceImpl();
        page page = noteService.allNote(1, pageSize);
        if (currentPage==null || currentPage.equals("0")){
            currentPage = "1";
        }
        page = noteService.allNote(Integer.parseInt(currentPage), pageSize);
        List<Notetype> typeList = noteService.getTypeList();
        Map<String, Object> m =new HashMap<>();
        m.put("typeList",typeList);
        m.put("page",page);
        String noteJson = JSON.toJSONString(m, true);
        PrintWriter out = resp.getWriter();
        out.write(noteJson);
        out.close();
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
